package com.smart.home.system;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.commons.lang3.StringUtils;

public class DateUtil {
	   public static String formatDate(Date date, String pattern) {
	        if (date == null) return "";
	        if (pattern == null) pattern = "yyyy-MM-dd";
	        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
	        return (sdf.format(date));
	    }

	    public static String formatDateTime(Date date) {
	        return (formatDate(date, "yyyy-MM-dd HH:mm:ss"));
	    }

	    public static String formatDateTime() {
	        return (formatDate(now(), "yyyy-MM-dd HH:mm:ss"));
	    }
	    
	    public static String formatDateTime2() {
	        return (formatDate(now(), "yyyyMMddHHmmss"));
	    }

	    public static String formatDate(Date date) {
	        return (formatDate(date, "yyyy-MM-dd"));
	    }

	    public static String formatDate() {
	        return (formatDate(now(), "yyyy-MM-dd"));
	    }
	    
	    /**
	     * 
	     * @return
	     */
	    public static String formatDate2() {
	        return (formatDate(now(), "yyyyMMdd"));
	    }

	    public static String formatTime(Date date) {
	        return (formatDate(date, "HH:mm:ss"));
	    }

	    public static String formatTime() {
	        return (formatDate(now(), "HH:mm:ss"));
	    }

	    public static String formatTime2() {
	        return (formatDate(now(), "HHmmss"));
	    }

	    public static Date now() {
	        return (new Date());
	    }

	    public static Date parseDateTime(String datetime) {
	        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	        if ((datetime == null) || (datetime.equals(""))) {
	            return null;
	        } else {
	            try {
	                return formatter.parse(datetime);
	            } catch (ParseException e) {
	                return parseDate(datetime);
	            }
	        }
	    }

	    public static Date parseDate(String date) {
	        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");

	        if ((date == null) || (date.equals(""))) {
	            return null;
	        } else {
	            try {
	                return formatter.parse(date);
	            } catch (ParseException e) {
	                return null;
	            }
	        }
	    }
	    
	    public static Date parseDate2(String date){
	    	SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");

	        if ((date == null) || (date.equals(""))) {
	            return null;
	        } else {
	            try {
	                return formatter.parse(date);
	            } catch (ParseException e) {
	                return null;
	            }
	        }
	    }

	    public static Date parseDate(Date datetime) {
	        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");

	        if (datetime == null) {
	            return null;
	        } else {
	            try {
	                return formatter.parse(formatter.format(datetime));
	            } catch (ParseException e) {
	                return null;
	            }
	        }
	    }

	    public static String formatDate(Object o) {
	        if (o == null) return "";
	        if (o.getClass() == String.class)
	            return formatDate(o);
	        else if (o.getClass() == Date.class)
	            return formatDate((Date) o);
	        else if (o.getClass() == Timestamp.class) {
	            return formatDate(new Date(((Timestamp) o).getTime()));
	        } else
	            return o.toString();
	    }

	    public static String formatDateTime(Object o) {
	        if (o.getClass() == String.class)
	            return formatDateTime(o);
	        else if (o.getClass() == Date.class)
	            return formatDateTime((Date) o);
	        else if (o.getClass() == Timestamp.class) {
	            return formatDateTime(new Date(((Timestamp) o).getTime()));
	        } else
	            return o.toString();
	    }

	    /**
	     * 给时间加上或减去指定毫秒，秒，分，时，天、月或年等，返回变动后的时间
	     *
	     * @param date   要加减前的时间，如果不传，则为当前日期
	     * @param field  时间域，有Calendar.MILLISECOND,Calendar.SECOND,Calendar.MINUTE,<br>
	     *               Calendar.HOUR,Calendar.DATE, Calendar.MONTH,Calendar.YEAR
	     * @param amount 按指定时间域加减的时间数量，正数为加，负数为减。
	     * @return 变动后的时间
	     */
	    public static Date add(Date date, int field, int amount) {
	        if (date == null) {
	            date = new Date();
	        }

	        Calendar cal = Calendar.getInstance();
	        cal.setTime(date);
	        cal.add(field, amount);

	        return cal.getTime();
	    }

	    public static Date addMilliSecond(Date date, int amount) {
	        return add(date, Calendar.MILLISECOND, amount);
	    }

	    public static Date addSecond(Date date, int amount) {
	        return add(date, Calendar.SECOND, amount);
	    }

	    public static Date addMiunte(Date date, int amount) {
	        return add(date, Calendar.MINUTE, amount);
	    }

	    public static Date addHour(Date date, int amount) {
	        return add(date, Calendar.HOUR, amount);
	    }

	    public static Date addDay(Date date, int amount) {
	        return add(date, Calendar.DATE, amount);
	    }

	    public static Date addMonth(Date date, int amount) {
	        return add(date, Calendar.MONTH, amount);
	    }

	    public static Date addYear(Date date, int amount) {
	        return add(date, Calendar.YEAR, amount);
	    }

	    public static Date getDate() {
	        return parseDate(formatDate());
	    }
	    
	    public static String getDatePath(Date d) {
			Calendar c = Calendar.getInstance();
			c.setTime(d);
			StringBuffer path = new StringBuffer();
			path.append("/");
			path.append(c.get(Calendar.YEAR));
			path.append("/");
			path.append(c.get(Calendar.MONTH) + 1);
			path.append("/");
			path.append(c.get(Calendar.DATE));
			return path.toString();
		}

	    public static Date getDateTime() {
	        return parseDateTime(formatDateTime());
	    }
	    
		public static Date getDate(String dateStr) {
			Date temp1 = null;
			if (dateStr == null)
				return null;
			if (dateStr.equals(""))
				return null;
			SimpleDateFormat formatter = null;
			try {
				if (dateStr.indexOf(" ") != -1) {
					String[] aa = StringUtils.split(dateStr, ":");
					if (aa.length == 3) {
						formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					} else if (aa.length == 2) {
						formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
					} else {
						formatter = new SimpleDateFormat("yyyy-MM-dd HH");
					}
				} else {
					if (dateStr.indexOf("-") == -1) {
						formatter = new SimpleDateFormat("yyyyMMdd");
					} else {
						formatter = new SimpleDateFormat("yyyy-MM-dd");
					}
				}
				temp1 = formatter.parse(dateStr);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return temp1;
		}

		public static Date getDate(Object o) {
			if (o == null) {
				return null;
			} else if (o instanceof Date) {
				return (Date) o;
			} else if (o instanceof String) {
				return getDate(String.valueOf(o));
			} else if (o instanceof java.sql.Timestamp) {
				return new Date(((java.sql.Timestamp) o).getTime());
			} else if (o instanceof java.sql.Date) {
				return new Date(((java.sql.Date) o).getTime());
			} else {
				return null;
			}
		}
		
		public static String dateToStr(Date date, String format) {
			if (date == null) {
				return "";
			} else {
				SimpleDateFormat df = new SimpleDateFormat(format);
				return df.format(date);
			}
		}

		public static String dateToStr(Date date) {
			return dateToStr(date, "yyyy-MM-dd");
		}

		public static String dateToCNStr(Date date) {
			return dateToStr(date, "yyyy年MM月dd日");
		}

		public static String dateToStrYYYMMDD(Date date) {
			return dateToStr(date, "yyyyMMdd");
		}
		
		
	    @SuppressWarnings("deprecation")
		public static String getDateStrByTime(long time){
	    	Date date = new Date(time);
	    	return date.toLocaleString().substring(0,date.toLocaleString().indexOf(" "));
	    }
	    
	    public static Timestamp getNewTimestamp() {
	    	return new Timestamp(new Date().getTime());
	    }
	    
	    //返回传入日期本月第一天的0点0分0秒0毫秒
		public static Date firstOfMonth(Date date){
	    	Calendar cal=Calendar.getInstance();
	    	cal.setTime(date);
	    	cal.set(Calendar.DAY_OF_MONTH, 1);
	    	cal.set(Calendar.HOUR_OF_DAY, 0);
	    	cal.set(Calendar.MINUTE, 0);
	    	cal.set(Calendar.SECOND, 0);
	    	cal.set(Calendar.MILLISECOND, 0);
	    	return cal.getTime();
	    }
		
		//返回传入日期的本月最后一天的59分59秒
		public static Date endOfMonth(Date date){
			Calendar cal=Calendar.getInstance();
			cal.setTime(date);
			cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
			cal.set(Calendar.HOUR_OF_DAY, 23);
			cal.set(Calendar.MINUTE, 59);
			cal.set(Calendar.SECOND, 59);
			cal.set(Calendar.MILLISECOND,999);
			return cal.getTime();
		}
		//获取本周一的0点0分0秒
		public static Date firstOfWeek(Date date){
			Calendar cal=Calendar.getInstance();
			cal.add(Calendar.DAY_OF_MONTH, -1);
			cal.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);
			cal.set(Calendar.HOUR_OF_DAY, 0);
			cal.set(Calendar.MINUTE, 0);
			cal.set(Calendar.SECOND, 0);
			cal.set(Calendar.MILLISECOND, 0);
			return cal.getTime();
		}
		//获取本周末的最后一秒
		public static Date endOfWeek(Date date){
			Calendar cal=Calendar.getInstance();
			cal.add(Calendar.DAY_OF_MONTH, -1);
			cal.set(Calendar.DAY_OF_WEEK,Calendar.SUNDAY);
			cal.set(Calendar.HOUR_OF_DAY, 23);
			cal.set(Calendar.MINUTE, 59);
			cal.set(Calendar.SECOND, 59);
			cal.set(Calendar.MILLISECOND, 999);
			return cal.getTime();
		}
		//将传入日期修改为当天的0点0分0秒
		public static Date firstOfDay(Date date){
			Calendar cal=Calendar.getInstance();
			cal.setTime(date);
			cal.set(Calendar.HOUR_OF_DAY, 0);
	    	cal.set(Calendar.MINUTE, 0);
	    	cal.set(Calendar.SECOND, 0);
	    	cal.set(Calendar.MILLISECOND, 0);
			return cal.getTime();
		}
		//将传入的值修改为当天的最后一秒
		public static Date endOfDay(Date date){
			Calendar cal=Calendar.getInstance();
			cal.setTime(date);
			cal.set(Calendar.HOUR_OF_DAY, 23);
			cal.set(Calendar.MINUTE, 59);
			cal.set(Calendar.SECOND, 59);
			cal.set(Calendar.MILLISECOND,999);
			return cal.getTime();
		}


}
